/*****************************************************************************
*        描述 :
*             文件描述
*        CLR版本:            3.5
*        创建者 :            蒋 光
*        个人域名:           jiangguang.net.cn
*        命名空间名称:       ECP.DataAccess
*        文件名:             BaseUserDao.cs
*        创建系统时间:       2012/11/2 19:07:00
*        创建年份:           2012
*
/*****************************************************************************/

namespace ECP.DataAccess
{
    using System.Data;
    using ECP.DataAccess.Utilities;
    using ECP.Model;
    using ECP.Utilities;

    public class BaseUserDao : BaseDao
    {
        public BaseUserDao()
        {
            base.CurrentTableName = BaseUserTable.TableName;
        }

        public BaseUserDao(IDbHelper dbHelper)
            : this()
        {
            base.DbHelper = dbHelper;
        }

        public BaseUserDao(IDbHelper dbHelper, BaseUserInfo userInfo)
            : this(dbHelper)
        {
            base.UserInfo = userInfo;
        }

        public string AddEntity(BaseUserEntity userEntity)
        {
            SQLBuilder builder = new SQLBuilder(base.DbHelper);
            builder.BeginInsert(BaseUserTable.TableName);
            builder.SetValue(BaseUserTable.FieldID, userEntity.ID);
            builder.SetValue(BaseUserTable.FieldUsername, userEntity.Username);
            builder.SetValue(BaseUserTable.FieldRealname, userEntity.Realname);
            builder.SetValue(BaseUserTable.FieldRole, userEntity.Role);
            builder.SetValue(BaseUserTable.FieldUserPassword, userEntity.UserPassword);
            builder.SetValue(BaseUserTable.FieldTheme, userEntity.Theme);
            builder.SetValue(BaseUserTable.FieldIsStaff, userEntity.IsStaff ? 1 : 0);
            builder.SetValue(BaseUserTable.FieldSex, userEntity.Sex);
            builder.SetValue(BaseUserTable.FieldBirthday, userEntity.Birthday);
            builder.SetValue(BaseUserTable.FieldMobile, userEntity.Mobile);
            builder.SetValue(BaseUserTable.FieldOICQ, userEntity.OICQ);
            builder.SetValue(BaseUserTable.FieldEmail, userEntity.Email);
            builder.SetValue(BaseUserTable.FieldCompany, userEntity.Company);
            builder.SetValue(BaseUserTable.FieldDepartment, userEntity.Department);
            builder.SetValue(BaseUserTable.FieldHomeAddress, userEntity.HomeAddress);
            builder.SetValue(BaseUserTable.FieldUserOnLine, userEntity.UserOnLine);
            builder.SetValue(BaseUserTable.FieldIPAddress, userEntity.IPAddress);
            builder.SetValue(BaseUserTable.FieldMACAddress, userEntity.MACAddress);
            builder.SetValue(BaseUserTable.FieldSortCode, userEntity.ID);
            builder.SetValue(BaseUserTable.FieldDescription, userEntity.Description);
            builder.SetValue(BaseUserTable.FieldEnabled, userEntity.Enabled ? 1 : 0);
            builder.SetValue(BaseUserTable.FieldCreateUserID, base.UserInfo.ID);
            builder.SetDBNow(BaseUserTable.FieldCreateDate);
            builder.SetValue(BaseUserTable.FieldQuestion, userEntity.Question);
            builder.SetValue(BaseUserTable.FieldAnswerQuestion, userEntity.AnswerQuestion);
            if (builder.EndInsert() <= 0)
            {
                return string.Empty;
            }
            return userEntity.ID;
        }

        public string AddUser(string company, string department, string workgroup, string userName, string realName, string code, string role, string password, string email, bool enabled, string description, out string statusCode)
        {
            string str = string.Empty;
            if (this.Exists(BaseUserTable.FieldUsername, userName) || DbLogic.Exists(base.DbHelper, BaseStaffTable.TableName, BaseStaffTable.FieldUsername, userName))
            {
                statusCode = StatusCode.ErrorUsernameExist.ToString();
                return str;
            }
            if ((code.Length > 0) && this.Exists(BaseUserTable.FieldCode, code))
            {
                statusCode = StatusCode.ErrorCodeExist.ToString();
                return str;
            }
            if ((code.Length > 0) && DbLogic.Exists(base.DbHelper, BaseStaffTable.TableName, BaseStaffTable.FieldCode, code))
            {
                statusCode = StatusCode.ErrorCodeExist.ToString();
                return str;
            }
            str = this.AddUserEntity(string.Empty, company, department, workgroup, userName, realName, code, role, password, email, enabled, description);
            statusCode = StatusCode.OKAdd.ToString();
            return str;
        }

        public string AddUserEntity(string id, string company, string department, string workgroup, string userName, string realname, string code, string role, string password, string email, bool enabled, string description)
        {
            int num = 0;
            if (string.IsNullOrEmpty(id))
            {
                id = BaseSequenceDao.Instance.GetSequence(base.DbHelper, BaseStaffTable.TableName);
            }
            SQLBuilder builder = new SQLBuilder(base.DbHelper);
            builder.BeginInsert(BaseUserTable.TableName);
            builder.SetValue(BaseUserTable.FieldID, id);
            builder.SetValue(BaseUserTable.FieldCompany, company);
            builder.SetValue(BaseUserTable.FieldDepartment, department);
            builder.SetValue(BaseUserTable.FieldWorkgroup, workgroup);
            builder.SetValue(BaseUserTable.FieldUsername, userName);
            builder.SetValue(BaseUserTable.FieldRealname, realname);
            builder.SetValue(BaseUserTable.FieldCode, code);
            builder.SetValue(BaseUserTable.FieldUserPassword, password);
            builder.SetValue(BaseUserTable.FieldRole, role);
            builder.SetValue(BaseUserTable.FieldEmail, email);
            builder.SetValue(BaseUserTable.FieldEnabled, enabled ? 1 : 0);
            builder.SetValue(BaseUserTable.FieldAuditStatus, enabled ? AuditStatus.AuditPass.ToString() : AuditStatus.WaitForAudit.ToString());
            builder.SetValue(BaseUserTable.FieldDescription, description);
            builder.SetValue(BaseUserTable.FieldSortCode, id);
            builder.SetValue(BaseUserTable.FieldCreateUserID, base.UserInfo.ID);
            builder.SetDBNow(BaseUserTable.FieldCreateDate);
            num += builder.EndInsert();
            return ((num > 0) ? id : string.Empty);
        }

        public int BatchSetPassword(string[] userIDs, string password, out string statusCode)
        {
            int num = 0;
            if (!BaseSystemInfo.AllowNullPassword && (password.Length == 0))
            {
                statusCode = StatusCode.PasswordCanNotBeNull.ToString();
                return num;
            }
            if (BaseSystemInfo.EncryptPassword)
            {
                password = BaseCodeChange.CodeChange(password);
            }
            for (int i = 0; i < userIDs.Length; i++)
            {
                num = DbLogic.SetProperty(base.DbHelper, BaseUserTable.TableName, BaseUserTable.FieldID, (object[])userIDs, BaseUserTable.FieldUserPassword, password);
            }
            if (num > 0)
            {
                statusCode = StatusCode.SetPasswordOK.ToString();
                return num;
            }
            statusCode = StatusCode.ErrorDeleted.ToString();
            return num;
        }

        private int ChangeOnLine(string id)
        {
            int num = 0;
            if ((base.UserInfo.ID.Length > 0) && !base.UserInfo.ID.Equals(id))
            {
                num += this.OnExit(base.UserInfo.ID);
                num += this.OnLine(id);
            }
            return num;
        }

        public int ChangePassword(string oldPassword, string newPassword, out string statusCode)
        {
            int num = 0;
            if (!BaseSystemInfo.AllowNullPassword && string.IsNullOrEmpty(newPassword))
            {
                statusCode = StatusCode.PasswordCanNotBeNull.ToString();
                return num;
            }
            if (BaseSystemInfo.EncryptPassword)
            {
                oldPassword = BaseCodeChange.CodeChange(oldPassword);
                newPassword = BaseCodeChange.CodeChange(newPassword);
            }
            BaseUserEntity entity = new BaseUserEntity();
            entity.GetFrom(this.Get(base.UserInfo.ID));
            if (((entity.UserPassword != null) || (oldPassword.Length != 0)) && !entity.UserPassword.Equals(oldPassword))
            {
                statusCode = StatusCode.OldPasswordError.ToString();
                return num;
            }
            num = this.SetProperty(base.UserInfo.ID, BaseUserTable.FieldUserPassword, newPassword);
            if (num == 1)
            {
                statusCode = StatusCode.ChangePasswordOK.ToString();
                return num;
            }
            statusCode = StatusCode.ErrorDeleted.ToString();
            return num;
        }

        private bool CheckIPAddress(string[] ipAddress)
        {
            for (int i = 0; i < ipAddress.Length; i++)
            {
                if (this.CheckIPAddress(ipAddress[i]))
                {
                    return true;
                }
            }
            return false;
        }

        private bool CheckIPAddress(string ipAddress)
        {
            string[] names = new string[] { BaseParameterTable.FieldParameterID, BaseParameterTable.FieldCategoryID, BaseParameterTable.FieldParameterCode, BaseParameterTable.FieldEnabled };
            object[] values = new object[] { base.UserInfo.ID, "IPAddress", ipAddress, 1 };
            return DbLogic.Exists(base.DbHelper, BaseParameterTable.TableName, names, values);
        }

        private bool CheckMacAddress(string[] macAddress)
        {
            for (int i = 0; i < macAddress.Length; i++)
            {
                if (this.CheckMacAddress(macAddress[i]))
                {
                    return true;
                }
            }
            return false;
        }

        private bool CheckMacAddress(string macAddress)
        {
            string[] names = new string[] { BaseParameterTable.FieldParameterID, BaseParameterTable.FieldCategoryID, BaseParameterTable.FieldParameterCode, BaseParameterTable.FieldEnabled };
            object[] values = new object[] { base.UserInfo.ID, "MacAddress", macAddress, 1 };
            return DbLogic.Exists(base.DbHelper, BaseParameterTable.TableName, names, values);
        }

        public int CheckOnLine()
        {
            int num = 0;
            if (BaseSystemInfo.UpdateVisit)
            {
                string sqlQuery = string.Empty;
                switch (BaseConfiguration.Instance.DataBaseType)
                {
                    case DataBaseType.Oracle:
                        sqlQuery = " UPDATE " + BaseUserTable.TableName + "     SET " + BaseUserTable.FieldUserOnLine + " = 0    WHERE (" + BaseUserTable.FieldLastVisit + " IS NULL)       OR ((" + BaseUserTable.FieldUserOnLine + " > 0) AND (" + BaseUserTable.FieldLastVisit + " IS NOT NULL) AND ((SYSDATE - " + BaseUserTable.FieldLastVisit + ") * 24 * 60 * 60 > " + BaseSystemInfo.OnLineTime0ut.ToString() + "))";
                        return (num + base.DbHelper.ExecuteNonQuery(sqlQuery));

                    case DataBaseType.Sqlserver:
                        sqlQuery = " UPDATE " + BaseUserTable.TableName + "     SET " + BaseUserTable.FieldUserOnLine + " = 0    WHERE (" + BaseUserTable.FieldLastVisit + " IS NULL)       OR ((" + BaseUserTable.FieldUserOnLine + " > 0) AND (" + BaseUserTable.FieldLastVisit + " IS NOT NULL) AND (DATEADD (s, " + BaseSystemInfo.OnLineTime0ut.ToString() + ", " + BaseUserTable.FieldLastVisit + ") < " + base.DbHelper.GetDBNow() + "))";
                        return (num + base.DbHelper.ExecuteNonQuery(sqlQuery));

                    case DataBaseType.Access:
                    case DataBaseType.Bd2:
                        return num;

                    case DataBaseType.MySql:
                        sqlQuery = " UPDATE " + BaseUserTable.TableName + "     SET " + BaseUserTable.FieldUserOnLine + " = 0    WHERE (" + BaseUserTable.FieldLastVisit + " IS NULL)       OR ((" + BaseUserTable.FieldUserOnLine + " > 0) AND (" + BaseUserTable.FieldLastVisit + " IS NOT NULL) AND (DATE_ADD(" + BaseUserTable.FieldLastVisit + ", Interval " + BaseSystemInfo.OnLineTime0ut.ToString() + " SECOND) < " + base.DbHelper.GetDBNow() + "))";
                        return (num + base.DbHelper.ExecuteNonQuery(sqlQuery));
                }
            }
            return num;
        }

        private bool CheckOnLineLimit()
        {
            bool flag = false;
            string sqlQuery = string.Empty;
            sqlQuery = " SELECT COUNT(*)    FROM " + BaseUserTable.TableName + "  WHERE " + BaseUserTable.FieldUserOnLine + " > 0 ";
            object obj2 = base.DbHelper.ExecuteScalar(sqlQuery);
            if ((obj2 != null) && (BaseSystemInfo.OnLineLimit < int.Parse(obj2.ToString())))
            {
                flag = true;
            }
            return flag;
        }

        public int CheckUserStaff()
        {
            string sqlQuery = " UPDATE ECP_Staff SET UserID = null WHERE UserID NOT IN ( SELECT id FROM ECP_USER ) ";
            return base.DbHelper.ExecuteNonQuery(sqlQuery);
        }

        public DataTable Get(string id, BaseUserEntity userEntity)
        {
            DataTable dataTable = this.Get(id);
            userEntity.GetFrom(dataTable);
            return dataTable;
        }

        public DataTable GetDT()
        {
            DataTable dataTable = new DataTable(BaseUserTable.TableName);
            string str2 = " SELECT " + BaseUserTable.TableName + ".*         , ( SELECT " + BaseRoleTable.FieldRealname + "  FROM " + BaseRoleTable.TableName + " WHERE " + BaseRoleTable.FieldID + " = " + BaseUserTable.TableName + "." + BaseUserTable.FieldRole + ") AS RoleFullName    FROM " + BaseUserTable.TableName;
            string sqlQuery = str2 + " ORDER BY " + BaseUserTable.TableName + "." + BaseUserTable.FieldSortCode;
            base.DbHelper.Fill(dataTable, sqlQuery);
            return dataTable;
        }

        public DataTable GetDTByIDs(string[] userIDs)
        {
            DataTable dataTable = new DataTable(BaseUserTable.TableName);
            string sqlQuery = ((" SELECT " + BaseUserTable.TableName + ".*         , ( SELECT " + BaseRoleTable.FieldRealname + "  FROM " + BaseRoleTable.TableName + " WHERE " + BaseRoleTable.FieldID + " = " + BaseUserTable.TableName + "." + BaseUserTable.FieldRole + ") AS RoleFullName    FROM " + BaseUserTable.TableName) + " WHERE ID IN (" + BaseBusinessLogic.ObjectsToList(userIDs) + ")") + " ORDER BY " + BaseUserTable.FieldSortCode;
            base.DbHelper.Fill(dataTable, sqlQuery);
            return dataTable;
        }

        public BaseUserEntity GetEntity(string id)
        {
            return new BaseUserEntity(this.Get(id));
        }

        public string GetOnLineCount()
        {
            string sqlQuery = " SELECT COUNT(UserOnLine) AS OnLine    FROM ECP_USER   WHERE UserOnLine = 1 AND Enabled = 1";
            return base.DbHelper.ExecuteScalar(sqlQuery).ToString();
        }

        public DataTable GetOnLineStateList()
        {
            string sqlQuery = " SELECT " + BaseUserTable.TableName + "." + BaseUserTable.FieldID + ", " + BaseUserTable.TableName + "." + BaseUserTable.FieldUserOnLine + " FROM " + BaseUserTable.TableName + " JOIN " + BaseStaffTable.TableName + " ON " + BaseUserTable.TableName + ".ID = " + BaseStaffTable.TableName + ".ID  WHERE " + BaseUserTable.TableName + "." + BaseUserTable.FieldEnabled + " = 1  AND " + BaseUserTable.TableName + "." + BaseUserTable.FieldIsVisible + " = 1  AND (" + BaseStaffTable.TableName + "." + BaseStaffTable.FieldDepartmentID + " IN (SELECT " + BaseOrganizeTable.FieldID + " FROM " + BaseOrganizeTable.TableName + " WHERE " + BaseOrganizeTable.FieldIsInnerOrganize + " = 1)  OR " + BaseStaffTable.TableName + "." + BaseStaffTable.FieldCompanyID + " IN (SELECT " + BaseOrganizeTable.FieldID + " FROM " + BaseOrganizeTable.TableName + " WHERE " + BaseOrganizeTable.FieldIsInnerOrganize + " = 1)) ";
            DataTable dataTable = new DataTable(BaseUserTable.TableName);
            base.DbHelper.Fill(dataTable, sqlQuery);
            return dataTable;
        }

        public string[] GetUserIDs(string organizeID)
        {
            string[] strArray = null;
            string[] strArray2 = null;
            string[] strArray3 = null;
            if (!string.IsNullOrEmpty(organizeID))
            {
                BaseStaffDao dao = new BaseStaffDao(base.DbHelper);
                strArray = dao.GetIDs(BaseStaffTable.FieldCompanyID, organizeID, BaseStaffTable.FieldUserID);
                strArray2 = dao.GetIDs(BaseStaffTable.FieldDepartmentID, organizeID, BaseStaffTable.FieldUserID);
                strArray3 = dao.GetIDs(BaseStaffTable.FieldWorkgroupID, organizeID, BaseStaffTable.FieldUserID);
            }
            return BaseBusinessLogic.Concat(new string[][] { strArray, strArray2, strArray3 });
        }

        public string[] GetUserIDs(string[] organizeIDs, string[] roleIDs)
        {
            string[] strArray = null;
            string[] strArray2 = null;
            string[] strArray3 = null;
            if (organizeIDs != null)
            {
                BaseStaffDao dao = new BaseStaffDao(base.DbHelper);
                strArray = dao.GetIDs(BaseStaffTable.FieldCompanyID, (object[])organizeIDs, BaseStaffTable.FieldUserID);
                strArray2 = dao.GetIDs(BaseStaffTable.FieldDepartmentID, (object[])organizeIDs, BaseStaffTable.FieldUserID);
                strArray3 = dao.GetIDs(BaseStaffTable.FieldWorkgroupID, (object[])organizeIDs, BaseStaffTable.FieldUserID);
            }
            string[] strArray4 = null;
            if (roleIDs != null)
            {
                strArray4 = new BaseUserRoleDao(base.DbHelper).GetIDs(BaseUserRoleTable.FieldRoleID, (object[])roleIDs, BaseUserRoleTable.FieldUserID);
            }
            return BaseBusinessLogic.Concat(new string[][] { strArray, strArray2, strArray3, strArray4 });
        }

        public string[] GetUserIDs(string[] userIDs, string[] organizeIDs, string[] roleIDs)
        {
            return BaseBusinessLogic.Concat(new string[][] { userIDs, this.GetUserIDs(organizeIDs, roleIDs) });
        }

        public BaseUserInfo Impersonation(string id, out string statusCode)
        {
            BaseUserInfo userInfo = null;
            DataTable dataTable = this.Get(id);
            BaseUserEntity entity = new BaseUserEntity();
            entity.GetFrom(dataTable);
            if ((!base.UserInfo.ID.Equals(base.ID) && BaseSystemInfo.CheckOnLine) && (entity.UserOnLine > 0))
            {
                statusCode = StatusCode.ErrorOnLine.ToString();
                return userInfo;
            }
            userInfo = entity.ConvertToUserInfo();
            if (entity.IsStaff)
            {
                BaseStaffEntity staffEntity = new BaseStaffEntity();
                BaseStaffDao dao = new BaseStaffDao(base.DbHelper, base.UserInfo);
                DataTable table2 = dao.Get(id);
                staffEntity.GetFrom(table2);
                userInfo = dao.ConvertToUserInfo(staffEntity, userInfo);
            }
            statusCode = StatusCode.OK.ToString();
            this.ChangeOnLine(id);
            return userInfo;
        }

        public BaseUserInfo Login(string userName, string password, string ipAddress, out string statusCode)
        {
            return this.Login(userName, password, ipAddress, string.Empty, out statusCode);
        }

        public BaseUserInfo Login(string userName, string password, string ipAddress, string macAddress, out string statusCode)
        {
            BaseUserInfo userInfo = null;
            string sqlQuery = " SELECT " + BaseUserTable.TableName + ".*         , ( SELECT " + BaseRoleTable.FieldRealname + "  FROM " + BaseRoleTable.TableName + " WHERE " + BaseRoleTable.FieldID + " = " + BaseUserTable.TableName + "." + BaseUserTable.FieldRole + ") AS RoleFullName    FROM " + BaseUserTable.TableName + "  WHERE " + BaseUserTable.FieldUsername + " = " + base.DbHelper.GetParameter(BaseUserTable.FieldUsername);
            string[] targetFileds = new string[1];
            object[] targetValues = new object[1];
            DataTable dataTable = new DataTable(BaseUserTable.TableName);
            targetFileds[0] = BaseUserTable.FieldUsername;
            targetValues[0] = userName;
            base.DbHelper.Fill(dataTable, sqlQuery, base.DbHelper.MakeParameters(targetFileds, targetValues));
            if (BaseSystemInfo.EncryptPassword)
            {
                BaseCodeChange.CodeChange(password);
            }
            statusCode = StatusCode.UserNotFound.ToString();
            BaseUserEntity entity = null;
            foreach (DataRow row in dataTable.Rows)
            {
                entity = new BaseUserEntity(row);
                if (!entity.Enabled)
                {
                    statusCode = StatusCode.LoginDeny.ToString();
                    return userInfo;
                }
                if (((base.UserInfo != null) && !base.UserInfo.ID.Equals(entity.ID)) && (BaseSystemInfo.CheckOnLine && (entity.UserOnLine > 0)))
                {
                    statusCode = StatusCode.ErrorOnLine.ToString();
                    return userInfo;
                }
                if (((entity.UserPassword != null) || (password.Length != 0)) && ((entity.UserPassword == null) || !entity.UserPassword.Equals(password)))
                {
                    if (BaseSystemInfo.RecordLoginLog)
                    {
                        BaseLogDao.Instance.Add(base.DbHelper, entity.ID, entity.Username, "Login", "Login", "Login", entity.Realname, ipAddress, "密码错误，登录被拒绝。");
                    }
                    statusCode = StatusCode.PasswordError.ToString();
                    return userInfo;
                }
                if ((BaseSystemInfo.CheckOnLineLimit && (BaseSystemInfo.OnLineLimit > 0)) && this.CheckOnLineLimit())
                {
                    statusCode = StatusCode.ErrorOnLineLimit.ToString();
                    return userInfo;
                }
                bool checkMACAddress = BaseSystemInfo.CheckMACAddress;
                bool checkIPAddress = BaseSystemInfo.CheckIPAddress;
                this.SetProperty(entity.ID, BaseUserTable.FieldIPAddress, ipAddress);
                this.SetProperty(entity.ID, BaseUserTable.FieldMACAddress, macAddress);
                statusCode = StatusCode.OK.ToString();
                this.ChangeOnLine(entity.ID);
                userInfo = entity.ConvertToUserInfo();
                if (entity.IsStaff)
                {
                    BaseStaffDao dao = new BaseStaffDao(base.DbHelper, base.UserInfo);
                    BaseStaffEntity staffEntity = new BaseStaffEntity(dao.GetDT(BaseStaffTable.FieldUserID, entity.ID));
                    if (!string.IsNullOrEmpty(staffEntity.ID))
                    {
                        userInfo = dao.ConvertToUserInfo(staffEntity, userInfo);
                    }
                }
                userInfo.IPAddress = ipAddress;
                userInfo.MACAddress = macAddress;
                userInfo.Password = password;
            }
            if ((statusCode == StatusCode.UserNotFound.ToString()) && BaseSystemInfo.RecordLoginLog)
            {
                BaseLogDao.Instance.Add(base.DbHelper, userName, base.UserInfo.Username, "Login", "Login", "Login", userName, base.UserInfo.IPAddress, "Failure，没有找到用户名。");
            }
            if (statusCode == StatusCode.OK.ToString())
            {
                if (BaseSystemInfo.RecordLoginLog)
                {
                    BaseLogDao.Instance.Add(base.DbHelper, entity.ID, userInfo.Username, "Login", "Login", "Login", entity.Realname, base.UserInfo.IPAddress, "Succeed，登录成功。");
                }
                this.UpdateVisitDate(entity.ID);
            }
            return userInfo;
        }

        public BaseUserInfo LoginByUID(string suid, string ipAddress, out string statusCode)
        {
            return this.LoginByUID(suid, ipAddress, string.Empty, out statusCode);
        }

        public BaseUserInfo LoginByUID(string suid, string ipAddress, string macAddress, out string statusCode)
        {
            BaseUserInfo info = null;
            statusCode = StatusCode.UserNotFound.ToString();
            if (!string.IsNullOrEmpty(suid))
            {
                DataTable dataTable = new BaseUserDao(base.DbHelper).Get(BaseUserTable.FieldSUID, suid);
                if (dataTable.Rows.Count == 1)
                {
                    BaseUserEntity entity = new BaseUserEntity(dataTable);
                    info = this.Login(entity.Username, entity.UserPassword, ipAddress, macAddress, out statusCode);
                }
            }
            return info;
        }

        public BaseUserInfo LoginByUserName(string userName, string ipAddress, string macAddress, out string statusCode)
        {
            BaseUserInfo info = null;
            statusCode = StatusCode.UserNotFound.ToString();
            if (!string.IsNullOrEmpty(userName))
            {
                DataTable dataTable = new BaseUserDao(base.DbHelper).Get(BaseUserTable.FieldUsername, userName);
                if (dataTable.Rows.Count == 1)
                {
                    BaseUserEntity entity = new BaseUserEntity(dataTable);
                    info = this.Login(entity.Username, entity.UserPassword, ipAddress, macAddress, out statusCode);
                }
            }
            return info;
        }

        public int OnExit(string userID)
        {
            int num = 0;
            if (!BaseSystemInfo.UpdateVisit)
            {
                return num;
            }
            string sqlQuery = string.Empty;
            sqlQuery = " UPDATE " + BaseUserTable.TableName + " SET " + BaseUserTable.FieldPreviousVisit + " = " + BaseUserTable.FieldLastVisit + " , " + BaseUserTable.FieldUserOnLine + " = 0 , " + BaseUserTable.FieldLastVisit + " = " + base.DbHelper.GetDBNow() + "  WHERE (" + BaseUserTable.FieldID + " = '" + userID + "')";
            return (num + base.DbHelper.ExecuteNonQuery(sqlQuery));
        }

        public int OnLine(string userID)
        {
            int num = 0;
            if (!BaseSystemInfo.UpdateVisit)
            {
                return num;
            }
            string sqlQuery = string.Empty;
            sqlQuery = " UPDATE " + BaseUserTable.TableName + "    SET " + BaseUserTable.FieldUserOnLine + " = 1 , " + BaseUserTable.FieldLastVisit + " = " + base.DbHelper.GetDBNow() + "  WHERE (" + BaseUserTable.FieldID + " = '" + userID + "')";
            return (num + base.DbHelper.ExecuteNonQuery(sqlQuery));
        }

        public int Reset()
        {
            int num = 0;
            num += this.ResetData();
            return (num + this.ResetVisitInfo());
        }

        private int ResetData()
        {
            int num = 0;
            string sqlQuery = " DELETE FROM " + BaseUserTable.TableName + " WHERE ID NOT IN (SELECT ID FROM " + BaseStaffTable.TableName + ") ";
            num += base.DbHelper.ExecuteNonQuery(sqlQuery);
            sqlQuery = " UPDATE " + BaseUserTable.TableName + " SET SortCode = " + BaseStaffTable.TableName + ".SortCode  FROM " + BaseStaffTable.TableName + " WHERE " + BaseUserTable.TableName + ".ID = " + BaseStaffTable.TableName + ".ID ";
            return (num + base.DbHelper.ExecuteNonQuery(sqlQuery));
        }

        private int ResetVisitInfo()
        {
            SQLBuilder builder = new SQLBuilder(base.DbHelper);
            builder.BeginUpdate(BaseUserTable.TableName);
            builder.SetNull(BaseUserTable.FieldFirstVisit);
            builder.SetNull(BaseUserTable.FieldPreviousVisit);
            builder.SetNull(BaseUserTable.FieldLastVisit);
            builder.SetValue(BaseUserTable.FieldLoginCount, 0);
            return builder.EndUpdate();
        }

        public int ResetVisitInfo(string[] ids)
        {
            int num = 0;
            for (int i = 0; i < ids.Length; i++)
            {
                if (ids[i].Length > 0)
                {
                    num += this.ResetVisitInfo(ids[i]);
                }
            }
            return num;
        }

        private int ResetVisitInfo(string id)
        {
            SQLBuilder builder = new SQLBuilder(base.DbHelper);
            builder.BeginUpdate(BaseUserTable.TableName);
            builder.SetNull(BaseUserTable.FieldFirstVisit);
            builder.SetNull(BaseUserTable.FieldPreviousVisit);
            builder.SetNull(BaseUserTable.FieldLastVisit);
            builder.SetValue(BaseUserTable.FieldLoginCount, 0);
            builder.SetWhere(BaseUserTable.FieldID, id);
            return builder.EndUpdate();
        }

        public DataTable Search(string search, bool enabled, string role)
        {
            return this.Search(search, enabled ? AuditStatus.AuditPass.ToString() : AuditStatus.WaitForAudit.ToString(), role);
        }

        public DataTable Search(string search, string auditStates, string role)
        {
            search = BaseBusinessLogic.GetSearchString(search);
            string sqlQuery = " SELECT " + BaseUserTable.TableName + ".* ," + BaseRoleTable.TableName + "." + BaseRoleTable.FieldRealname + " AS RoleFullName  FROM " + BaseUserTable.TableName + "      LEFT OUTER JOIN " + BaseRoleTable.TableName + "      ON " + BaseUserTable.TableName + "." + BaseUserTable.FieldRole + " = " + BaseRoleTable.TableName + "." + BaseRoleTable.FieldID + " WHERE 1=1 ";
            if (!string.IsNullOrEmpty(search))
            {
                string str2 = sqlQuery;
                sqlQuery = str2 + " AND (" + BaseUserTable.TableName + "." + BaseUserTable.FieldUsername + " LIKE '" + search + "' OR " + BaseUserTable.TableName + "." + BaseUserTable.FieldCode + " LIKE '" + search + "' OR " + BaseRoleTable.TableName + "." + BaseRoleTable.FieldRealname + " LIKE '" + search + "' OR " + BaseUserTable.TableName + "." + BaseUserTable.FieldDepartment + " LIKE '" + search + "')";
            }
            if (!string.IsNullOrEmpty(auditStates))
            {
                string str3 = sqlQuery;
                sqlQuery = str3 + " AND (" + BaseUserTable.TableName + "." + BaseUserTable.FieldAuditStatus + " = '" + auditStates + "')";
            }
            if (!string.IsNullOrEmpty(role))
            {
                string str4 = sqlQuery;
                sqlQuery = str4 + " AND (" + BaseUserTable.TableName + "." + BaseUserTable.FieldRole + " = '" + role + "')";
            }
            if (!base.UserInfo.IsAdministrator && BaseSystemInfo.UseScopeAdmin)
            {
                string[] userIDs = new BaseResourcePermissionScopeDao(base.DbHelper, base.UserInfo).GetUserIDs(base.UserInfo.ID, "SystemAdmin");
                string str5 = sqlQuery;
                sqlQuery = str5 + " AND (" + BaseUserTable.TableName + "." + BaseUserTable.FieldID + " IN (" + BaseBusinessLogic.ObjectsToList(userIDs) + ")) ";
            }
            string str6 = sqlQuery;
            sqlQuery = str6 + " ORDER BY " + BaseUserTable.TableName + "." + BaseUserTable.FieldSortCode;
            DataTable dataTable = new DataTable(BaseUserTable.TableName);
            base.DbHelper.Fill(dataTable, sqlQuery);
            return dataTable;
        }

        public int SetPassword(string userID, string password)
        {
            int num = 0;
            if (!BaseSystemInfo.AllowNullPassword && (password.Length == 0))
            {
                base.ReturnStatusCode = StatusCode.PasswordCanNotBeNull.ToString();
                return num;
            }
            if (BaseSystemInfo.EncryptPassword)
            {
                password = BaseCodeChange.CodeChange(password);
            }
            num = this.SetProperty(userID, BaseUserTable.FieldUserPassword, password);
            if (num == 1)
            {
                base.ReturnStatusCode = StatusCode.SetPasswordOK.ToString();
                return num;
            }
            base.ReturnStatusCode = StatusCode.ErrorDeleted.ToString();
            return num;
        }

        public int UpdateEntity(BaseUserEntity userEntity)
        {
            SQLBuilder builder = new SQLBuilder(base.DbHelper);
            builder.BeginUpdate(BaseUserTable.TableName);
            builder.SetValue(BaseUserTable.FieldUsername, userEntity.Username);
            builder.SetValue(BaseUserTable.FieldRealname, userEntity.Realname);
            builder.SetValue(BaseUserTable.FieldRole, userEntity.Role);
            if ((userEntity.UserPassword != null) && (userEntity.UserPassword.Length > 0))
            {
                builder.SetValue(BaseUserTable.FieldUserPassword, userEntity.UserPassword);
            }
            builder.SetValue(BaseUserTable.FieldTheme, userEntity.Theme);
            builder.SetValue(BaseUserTable.FieldCode, userEntity.Code);
            builder.SetValue(BaseUserTable.FieldSex, userEntity.Sex);
            builder.SetValue(BaseUserTable.FieldBirthday, userEntity.Birthday);
            builder.SetValue(BaseUserTable.FieldMobile, userEntity.Mobile);
            builder.SetValue(BaseUserTable.FieldOICQ, userEntity.OICQ);
            builder.SetValue(BaseUserTable.FieldEmail, userEntity.Email);
            builder.SetValue(BaseUserTable.FieldCompany, userEntity.Company);
            builder.SetValue(BaseUserTable.FieldDepartment, userEntity.Department);
            builder.SetValue(BaseUserTable.FieldHomeAddress, userEntity.HomeAddress);
            builder.SetValue(BaseUserTable.FieldIsStaff, userEntity.IsStaff ? 1 : 0);
            builder.SetValue(BaseUserTable.FieldUserOnLine, userEntity.UserOnLine);
            builder.SetValue(BaseUserTable.FieldIPAddress, userEntity.IPAddress);
            builder.SetValue(BaseUserTable.FieldMACAddress, userEntity.MACAddress);
            builder.SetValue(BaseUserTable.FieldSortCode, userEntity.ID);
            builder.SetValue(BaseUserTable.FieldDescription, userEntity.Description);
            builder.SetValue(BaseUserTable.FieldEnabled, userEntity.Enabled ? 1 : 0);
            if (userEntity.Enabled)
            {
                builder.SetValue(BaseUserTable.FieldAuditStatus, AuditStatus.AuditPass.ToString());
            }
            builder.SetValue(BaseUserTable.FieldModifyUserID, base.UserInfo.ID);
            builder.SetDBNow(BaseUserTable.FieldModifyDate);
            builder.SetWhere(BaseUserTable.FieldID, userEntity.ID);
            return builder.EndUpdate();
        }

        private int UpdateEntity(string id, string userName, string realName, string code, string role, bool enabled)
        {
            int num = 0;
            SQLBuilder builder = new SQLBuilder(base.DbHelper);
            builder.BeginUpdate(BaseUserTable.TableName);
            builder.SetValue(BaseUserTable.FieldUsername, userName);
            builder.SetValue(BaseUserTable.FieldCode, code);
            builder.SetValue(BaseUserTable.FieldRealname, realName);
            builder.SetValue(BaseUserTable.FieldRole, role);
            if (enabled)
            {
                builder.SetValue(BaseUserTable.FieldAuditStatus, AuditStatus.AuditPass.ToString());
            }
            builder.SetValue(BaseUserTable.FieldEnabled, enabled ? 1 : 0);
            builder.SetValue(BaseUserTable.FieldModifyUserID, base.UserInfo.ID);
            builder.SetDBNow(BaseUserTable.FieldModifyDate);
            builder.SetWhere(BaseUserTable.FieldID, id);
            return (num + builder.EndUpdate());
        }

        public int UpdateUser(string id, string userName, string realName, string code, string role, bool enabled, out string statusCode)
        {
            int num = 0;
            if (this.Exists(BaseUserTable.FieldUsername, userName, id))
            {
                statusCode = StatusCode.ErrorUsernameExist.ToString();
                return num;
            }
            if ((code.Length > 0) && this.Exists(BaseUserTable.FieldCode, code, id))
            {
                statusCode = StatusCode.ErrorCodeExist.ToString();
                return num;
            }
            num = this.UpdateEntity(id, userName, realName, code, role, enabled);
            if (num == 0)
            {
                statusCode = StatusCode.ErrorDeleted.ToString();
                return num;
            }
            statusCode = StatusCode.OKUpdate.ToString();
            return num;
        }

        private int UpdateVisitDate(string userID)
        {
            int num = 0;
            if (!BaseSystemInfo.UpdateVisit)
            {
                return num;
            }
            string sqlQuery = string.Empty;
            sqlQuery = " UPDATE " + BaseUserTable.TableName + "    SET " + BaseUserTable.FieldFirstVisit + " = " + base.DbHelper.GetDBNow() + "  WHERE (" + BaseUserTable.FieldID + " = '" + userID + "') AND " + BaseUserTable.FieldFirstVisit + " IS NULL";
            num += base.DbHelper.ExecuteNonQuery(sqlQuery);
            string str2 = " UPDATE " + BaseUserTable.TableName + " SET " + BaseUserTable.FieldPreviousVisit + " = " + BaseUserTable.FieldLastVisit + " , " + BaseUserTable.FieldUserOnLine + " = 1 , " + BaseUserTable.FieldLastVisit + " = " + base.DbHelper.GetDBNow() + " , " + BaseUserTable.FieldLoginCount + " = " + BaseUserTable.FieldLoginCount + " + 1 ";
            sqlQuery = str2 + "  WHERE (" + BaseUserTable.FieldID + " = '" + userID + "')";
            num += base.DbHelper.ExecuteNonQuery(sqlQuery);
            sqlQuery = " UPDATE " + BaseUserTable.TableName + "    SET " + BaseUserTable.FieldSUID + " = '" + BaseSequenceDao.Instance.NewGuid() + "'  WHERE (" + BaseUserTable.FieldID + " = '" + userID + "')       AND " + BaseUserTable.FieldSUID + " IS NULL ";
            return (num + base.DbHelper.ExecuteNonQuery(sqlQuery));
        }
    }
}